package com.kabigon.tiny.factory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class CheckCompareFactory implements InitializingBean, ApplicationContextAware {

    private final Map<CheckEnum, ICheckStrategy> checkStrategyMap = new ConcurrentHashMap<>();

    private ApplicationContext applicationContext;

    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, ICheckStrategy> beans = applicationContext.getBeansOfType(ICheckStrategy.class);
        beans.values().forEach(checkStrategy -> checkStrategyMap.put(checkStrategy.getCheckEnum(), checkStrategy));
    }

    /**
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void checkCompare(CheckEnum checkEnum, String filePathA, String filePathB) {
        ICheckStrategy checkStrategy = checkStrategyMap.get(checkEnum);
        checkStrategy.check(filePathA, filePathB);
    }

}
